package com.kma.summer2012.client.view.manager;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import com.kma.summer2012.client.constants.ConstantsPresenter;
import com.kma.summer2012.client.presenters.catalog.CatalogWidgetPresenter;
import com.kma.summer2012.client.presenters.manager.AddToolPresenter;
import com.kma.summer2012.client.services.catalogOfWorks.CrumbsCatalogOfWorksService;
import com.kma.summer2012.client.styles.style2.Recources;
import com.kma.summer2012.client.view.catalog.CatalogWidgetView;
import com.kma.summer2012.shared.model.UICrumbsWorkItem;
import com.smartgwt.client.widgets.events.DoubleClickEvent;
import com.smartgwt.client.widgets.events.DoubleClickHandler;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.tree.TreeGrid;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Администратор
 * Date: 21.02.13
 * Time: 23:59
 * To change this template use File | Settings | File Templates.
 */
public class AddToolView extends Composite implements AddToolPresenter.Display {

    interface AddToolViewUiBinder extends UiBinder<Widget, AddToolView> {
    }

    private static AddToolViewUiBinder ourUiBinder = GWT.create(AddToolViewUiBinder.class);
    public static ConstantsPresenter constants;
    @UiField
    protected Label pageTitle;
    @UiField
    protected Button saveButton;
    @UiField
    protected Button cancelButton;
    @UiField
    protected TextBox toolName;
    @UiField
    protected TextArea toolDescription;

    @UiField
    protected ListBox selectedWorks;

    @UiField
    protected CatalogWidgetView catalog;

    @Override
    public void setPageTitle(String title) {
        pageTitle.setText(title);
    }

    @Override
    public String getToolName() {
        return toolName.getValue();
    }

    @Override
    public void setToolName(String toolName) {
        this.toolName.setValue(toolName);
    }

    @Override
    public String getToolDescription() {
        return toolDescription.getValue();
    }

    @Override
    public void setToolDescription(String toolDescription) {
        this.toolDescription.setValue(toolDescription);
    }

    @Override
    public List<String> getWorkList() {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < selectedWorks.getItemCount(); ++i) {
            list.add(selectedWorks.getItemText(i));
        }
        return list;
    }

    @Override
    public void setWorkList(List<String> list) {
        this.selectedWorks.clear();
        for (int i = 0; i < list.size(); ++i) {
            selectedWorks.addItem(list.get(i));
        }
    }

    @Override
    public HasClickHandlers getSaveButton() {
        return saveButton;
    }

    @Override
    public HasClickHandlers getCancelButton() {
        return cancelButton;
    }

    @Override
    public TreeGrid getCatalog() {
        return catalog.getTreeGrid();
    }


    public AddToolView() {
        HTMLPanel rootElement = (HTMLPanel) ourUiBinder.createAndBindUi(this);

        constants = GWT.create(ConstantsPresenter.class);

        pageTitle.setText(constants.TOOl_ADD());
        pageTitle.setStyleName(Recources.INSTANCE.styles().titleCatalog());

        saveButton.addStyleName(Recources.INSTANCE.styles().Caption());
        cancelButton.addStyleName(Recources.INSTANCE.styles().Caption());
        toolName.setStyleName(Recources.INSTANCE.styles().input());
        toolDescription.setStyleName(Recources.INSTANCE.styles().textarea());
        initWidget(rootElement);
        this.setToolName("");
        this.setToolDescription("");
        this.setWorkList(new ArrayList<String>());

        new CatalogWidgetPresenter(catalog);

        this.getCatalog().setWidth(300);
        this.getCatalog().setHeight(211);
        this.getCatalog().addStyleName(Recources.INSTANCE.styles().ShowToolTable());

        this.getCatalog().addDoubleClickHandler(new DoubleClickHandler() {
            @Override
            public void onDoubleClick(DoubleClickEvent doubleClickEvent) {
                ListGridRecord record = AddToolView.this.getCatalog().getSelectedRecord();
                if (record.getAttributeAsBoolean("IsParent") == false) {
                    int id = record.getAttributeAsInt("Id");
                    CrumbsCatalogOfWorksService.App.getInstance().getWorkById(id, new AsyncCallback<UICrumbsWorkItem>() {
                        @Override
                        public void onFailure(Throwable throwable) {
                            Window.alert("oh no");
                        }

                        @Override
                        public void onSuccess(UICrumbsWorkItem uiCrumbsWorkItem) {
                            if (!alreadyContains(selectedWorks, uiCrumbsWorkItem.getWorkName()))
                                AddToolView.this.selectedWorks.addItem(uiCrumbsWorkItem.getWorkName());

                        }
                    });
                }
            }
        });
        this.selectedWorks.setVisibleItemCount(13);
        this.catalog.setHeight(String.valueOf(this.selectedWorks.getOffsetHeight()));
        this.selectedWorks.addDoubleClickHandler(new com.google.gwt.event.dom.client.DoubleClickHandler() {
            @Override
            public void onDoubleClick(com.google.gwt.event.dom.client.DoubleClickEvent doubleClickEvent) {
                int selected = selectedWorks.getSelectedIndex();
                try {
                    selectedWorks.removeItem(selected);
                } catch (Exception e) {
                }
            }
        });


    }

    boolean alreadyContains(ListBox listBox, String string) {
        for (int i = 0; i < listBox.getItemCount(); ++i) {
            if (listBox.getItemText(i).equals(string))
                return true;
        }
        return false;
    }
}